<?php 
class Image{
	//水印的位置
	private $postion;
	
	//不需要构造函数
	
	public function thumb($src,$width=NULL,$height=NULL){
		$width = is_null($width) ? C('THUMB_WIDTH') : $width;
		$height = is_null($height) ? C('THUMB_HEIGHT') : $height;
		//打开目标图（创建画布）
		$dstImg = imagecreatetruecolor($width, $height);
		//类型
		$type = ltrim(strrchr($src, '.'),'.');
		if($type == 'jpg') $type = 'jpeg';
		$fn = "imagecreatefrom" . $type;
		//打开源图
		$srcImg = $fn($src);
		$srcW = imagesx($srcImg);
		$srcH = imagesy($srcImg);
		//缩略
		imagecopyresized($dstImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcW, $srcH);
		//组合缩略图名称
		
		$thumbName = str_replace('.' . $type, '_thumb.' . $type, $src);
		//保存缩略图
		imagejpeg($dstImg,$thumbName);
		//销毁
		imagedestroy($dstImg);
		imagedestroy($srcImg);
		echo '<script> location.href="'.__APP__.'?c=Register"</script>';
		// include __APP__.'?c=Register';
	}
	
	
	public function water($logo,$pic,$position=NULL,$opacity=NULL){
		//水印位置，9宫格
		$this->position = is_null($position) ? C('WATER_POSITION') : $position;
		//透明度
		$opacity = is_null($opacity) ? C('WATER_OPACITY') : $opacity;
		
		//打开源图
		$srcImg = imagecreatefromjpeg($logo);
		//获得源图宽高
		$srcW = imagesx($srcImg);
		$srcH = imagesy($srcImg);
		//打开目标图
		$dstImg = imagecreatefromjpeg($pic);
		//获得目标图宽高
		$dstW = imagesx($dstImg);
		$dstH = imagesy($dstImg);
		//把源图贴到目标图上去
		//0, 0代表要把源图全部贴上去
		//imagecopy和imagecopymerge 区别就是多了一个透明度
		//返回位置数组，里面包含xy的坐标
		$xyArr = $this->getXY($dstW,$dstH,$srcW,$srcH);
		
		imagecopymerge($dstImg, $srcImg, $xyArr['x'], $xyArr['y'], 0, 0, $srcW, $srcH,$opacity);
		//处理目标图片名称
		$type = ltrim(strrchr($pic, '.'),'.');
		$waterName = str_replace('.' . $type, '_water.' . $type, $pic);
		$_SESSION['dest']=$waterName;
		$_SESSION['image'] = "<img src='". __UPLOAD__.'/'. basename($waterName). " '/>";
	
		//输出图片
		imagejpeg($dstImg,$waterName);
		//销毁
		imagedestroy($dstImg);
		imagedestroy($srcImg);
		echo '<script> location.href="'.__APP__.'?c=Register"</script>';
	}
	
	/**
	 * 获得x,y坐标
	 */
	private function getXY($dstW,$dstH,$srcW,$srcH){
		switch ($this->position) {
			case 1:
				$x = 10;
				$y = 10;
				break;
			case 2:
				$x=($dstW-$srcW)/2;
				$y=10;
				break;
			case 3:
				$w=$dstW-$srcW-10;
				$h=10;
				break;
			case 4:
				$w=10;
				$h=($dstH-$srcH)/2;
				break;
			case 5:
				$x = ($dstW - $srcW) / 2;
				$y = ($dstH - $srcH) / 2;
				break;
			case 6:
				$w=$dstW-$srcW-10;
				$h=($dstH-$srcH)/2;
				break;
			case 7:
				$h=$dstH-$srcH-10;
				$w=10;
				break;
			case 8:
				$h=$dstH-$srcH-10;
				$w=($dstW-$srcW)/2;
				break;
			case 9:
				$x = $dstW - $srcW;
				$y = $dstH - $srcH;
				break;
			
			default:
				$x = mt_rand(0, $dstW - $srcW);
				$y = mt_rand(0, $dstH - $srcH);
				break;
		}
		$arr['x'] = $x;
		$arr['y'] = $y;
		return $arr;
	}
	
	
	
	
}

//$img = new Image();
//////把./kuaixuewang.jpg缩略成200 * 200
//$img->thumb('./kuaixuewang.jpg',100,100);

//把logo.png 加到 kuaixuewang.jpg上面去，默认右下角
//$img->water('logo.jpg','kuaixuewang.jpg');








 ?>